<?php 
Class wxkeyController extends appController
{
	/**
	 * 关键字列表
	 */
	public function index()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$sort['keyword'] = trim(yf_empty($_GET['k'], '', true));
		$art_list = Model('wxkey')->key_list($page, $sort);
		
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		$this->display('index.html');
	}
	
	/**
	 * 删除关键字
	 */
	public function del()
	{
		$this->admin_priv('del_key', 2);
		$id = yf_empty($_REQUEST['id'], '', true);
		if(is_array($id))
		{
			foreach($id as $k => $v)
			{
				$result = Model('wxkey')->del_key($v);
			}
		}
		else
		{
			$id = intval($id);
			$result = Model('wxkey')->del_key($id);
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示关键字编辑页面
	 */
	public function edit_key()
	{
		$this->admin_priv('edit_key', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$this->assign('id', $id);
		
		$menu_list = Model('wxrule')->get_rule_list();
        $this->assign('rule_list', $menu_list);
		$art_info = Model('wxkey')->get_key($id);
		$this->assign('art', $art_info);
		$this->assign('action', 'modified_key');
		$this->display('key.html');
	
	}
	
	/**
	 * 更新关键字信息
	 */
	public function modified_key()
	{
		$this->admin_priv('edit_key');
		$id = yf_empty(intval($_POST['id']), '', true);
	    $info['keyword'] = yf_empty($_POST['keyword'], '关键字名称不能为空');
		$info['rid'] = yf_empty(intval($_POST['rule_id']), '', true);
		$info['type'] = yf_empty($_POST['type'], 0, true);
		$data = array('error' => 1, 'content' => '');
		if(Model('wxkey')->check_key($info['keyword'], $id))
		{
		    $result=Model('wxkey')->update_key($info, $id);
		    if($result)
		   	{
				$data['error']=0;
				$data['content']=Lang('SUCCESS_EDIT');
		   	}
		   	else
		   	{
				$data['content']=Lang('INVALID_OPERATION');
		    }
		}
		$this->showMsg($data);
	}
	
	/**
	 * 显示添加关键字页面
	 */
    public function add_key()
	{
		$this->admin_priv('add_key', 2);
		$menu_list = Model('wxrule')->get_rule_list();
		
		$this->assign('rule_list', $menu_list);
		$this->assign('action','new_key');
		$this->display('key.html');
	}
	
	/**
	 * 插入关键字信息
	 */
	public function new_key()
	{
		$this->admin_priv('add_key');
		$info['keyword'] = yf_empty($_POST['keyword'], '关键字名称不能为空');
		$info['rid'] = yf_empty(intval($_POST['rule_id']), '', true);
		$info['status'] = yf_empty($_POST['status'], 1, true);
		$info['type'] = yf_empty($_POST['type'], 0, true);
	
		$data = array('error' => 1, 'content' => '');
		$res = Model('wxkey')->check_key($info['keyword']);
		if($res['error'] == 0)
		{
		   	if(Model('wxkey')->create_key($info))
		  	{
		  		$data['error'] = 0;
				$data['content'] = Lang('SUCCESS_ADD');
			}
		   	else
		  	{
				$data['content'] = Lang('INVALID_OPERATION');
		  	}
		}
		$this->showMsg($data);
	}
	
}